<?php
namespace app\api\model;
use think\Model;
class UserDegree extends Model
{

    /**
     * @param $degree_ids
     * @param $user_id
     * 存学位
     */
    public function add($degree_ids,$user_id){
        foreach ($degree_ids as $k=>$v){
            $addData = array(
                'degree_id'=>$v,
                'user_id'=>$user_id,
            );
            $this->insertGetId($addData);
        }
    }

    /**
     * @param $degree_ids
     * @param $user_id
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     * 修改学位
     */
    public function edit($degree_ids,$user_id){
        //首先全部改为0
        $this->where(array('user_id'=>$user_id))->update(array('status'=>0));
        foreach ($degree_ids as $k=>$v){
            //查询有没有
            $info = $this->where(array('user_id'=>$user_id,'degree_id'=>$v))->find();
            if($info){
                $this->where(array('user_id'=>$user_id,'degree_id'=>$v))->update(array('status'=>1));
            }else {
                $addData = array(
                    'degree_id' => $v,
                    'user_id' => $user_id,
                );
                $this->insertGetId($addData);
            }
        }
    }

    /**
     * @param $user_id
     * @return array
     * 通过用户id获取学位id
     */
    public function getDegreeIds($user_id){

        $degree_ids = $this->where(array('user_id'=>$user_id,'status'=>1))->column('degree_id');
        return $degree_ids;
    }


    /**
     * @param $degree_ids
     * @return array
     * 通过用户id获取学位id
     */
    public function get_user_id($degree_ids){

        $user_ids = $this->where(array('degree_id'=>array('in',$degree_ids),'status'=>1))->column('user_id');
        return $user_ids;
    }
}